package com.naizhuai.ui.management;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.naizhuai.R;
import com.naizhuai.network.exception.ApiException;
import com.naizhuai.ui.BaseActivity;
import com.naizhuai.ui.entity.management.DepartmentBean;
import com.naizhuai.ui.management.presenter.CompanyManageDataApiStoreIml;
import com.naizhuai.util.ConstantsState;
import com.naizhuai.util.DialogUtils;
import com.naizhuai.util.NaizhuAILog;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observer;

/**
 * 部门信息操作界面
 */
public class CompanyDepartmentOperatActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "CompanyDepartmentOperatActivity";

    public static final int RESULT_SAVE = 1000;
    public static final int RESULT_DELETE = 1001;

    @BindView(R.id.department_name)
    EditText departName;

    private DepartmentBean departEditBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBasicData();
    }


    private void initBasicData() {
        Bundle intentBundle = getIntent().getExtras();
        if (intentBundle != null) {
            departEditBean = (DepartmentBean) intentBundle.getSerializable(DepartmentBean.DEPARTMENT_KEY);
            initView();
        }
    }

    private void initView() {
        departName.setText(departEditBean.getDepartmentName());
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_company_department_operate_layout;
    }

    @OnClick({R.id.base_back_icon, R.id.save_btn, R.id.delete_member})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_back_icon:
                super.onBackPressed();
                break;
            case R.id.save_btn:
                if (!TextUtils.isEmpty(departName.getText().toString())) {
                    departEditBean.setDepartmentName(departName.getText().toString());
                    CompanyManageDataApiStoreIml.getInstance().updateTeamMsg(departEditBean.getDepartmentId(), departEditBean.getDepartmentName())
                            .subscribe(new Observer<String>() {
                                @Override
                                public void onCompleted() {

                                }

                                @Override
                                public void onError(Throwable e) {
                                    if (e instanceof ApiException) {
                                        ApiException ex = (ApiException) e;
                                        DialogUtils.showPromptDialog(CompanyDepartmentOperatActivity.this, ex.getDisplayMessage());
                                    } else
                                        shortToast(getString(R.string.submit_fail));
                                }

                                @Override
                                public void onNext(String result) {
                                    if (ConstantsState.isResultOk(result)) {
                                        shortToast(getString(R.string.submit_ok));
                                        setResult(RESULT_SAVE, subMitData());
                                        finish();
                                    } else
                                        shortToast(getString(R.string.submit_fail));
                                }
                            });

                } else
                    shortToast(getString(R.string.department_name_error));
                break;
            case R.id.delete_member:
                DialogUtils.showSureDialog(this, getResources().getText(R.string.delete_department_prompt).toString(), new View.OnClickListener() {
                    @Override
                    public void onClick(View dialog) {
                        showWaitDiaLog();
                        NaizhuAILog.e("departEditBean.getDepartmentId()=" + departEditBean.getDepartmentId());
                        CompanyManageDataApiStoreIml.getInstance().deleteTeamFromCompanyByIds(departEditBean.getDepartmentId()).subscribe(new Observer<String>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {
                                hideWaitDialog();
                                ApiException.showException(e,getActivityContext(),true);
                            }

                            @Override
                            public void onNext(String s) {
                                hideWaitDialog();
                                if (!ConstantsState.isResultOk(s)) {
                                    shortToast(getResources().getText(R.string.action_fail).toString());
                                } else {
                                    setResult(RESULT_DELETE, subMitData());
                                    finish();
                                }
                            }
                        });
                    }
                });
                break;
        }
    }

    private Intent subMitData() {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putSerializable(DepartmentBean.DEPARTMENT_KEY, departEditBean);
        intent.putExtras(bundle);
        return intent;
    }


}
